<template>
  <a-modal v-model:visible="modalVisible" @ok="handleOk" @cancel="handleCancel">
    <template #title> 新建计划 </template>
    <div>
      <a-form ref="FormData" layout="vertical" :model="form" auto-label-width>
        <a-form-item field="name" label="采购计划名称:" required>
          <a-input
            placeholder="请输入采购计划名称"
            v-model="form.name"
            allow-clear
          ></a-input>
        </a-form-item>
        <a-form-item label="采购计划编号:" required>
          <a-input
            placeholder="系统自动生成"
            disabled
            v-model="form.number"
            allow-clear
          ></a-input>
        </a-form-item>
      </a-form>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
  import { reactive, computed, ref, watchEffect } from 'vue';

  const props = withDefaults(
    defineProps<{
      visible: boolean;
    }>(),
    {
      visible: false,
    }
  );

  const emit = defineEmits(['update:visible', 'addPlanEmit']);
  const FormData = ref();
  // 判断用户是否显示弹出框
  const modalVisible = computed({
    get() {
      return props.visible;
    },
    set(visible: boolean) {
      emit('update:visible', visible);
    },
  });

  let form = reactive({
    name: '',
    number: '',
  });

  const rules = reactive({});

  // 确定
  const handleOk = async () => {
    FormData.value.validate((res) => {
      if (!res) {
        emit('addPlanEmit', form);
        modalVisible.value = false;
      }
    });
  };

  // 取消
  const handleCancel = () => {
    modalVisible.value = false;
  };

  watchEffect(() => {
    if (modalVisible.value) {
      form.name = '';
      form.number = '';
    }
  });
</script>

<style scoped lang="less"></style>
